package org.crystalrange.softwares.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.crystalrange.files.model.FileType;
import org.crystalrange.softwares.model.SoftwareVersion;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class SoftwareVersionController extends GenericForwardComposer {

	private SoftwareVersion softwareVersion;
	protected Listbox listboxFileType;
	protected AnnotateDataBinder binder;
	private List<FileType> fileTypes;
	private String name;
	private String description;

	public SoftwareVersion getSoftwareVersion() {
		return softwareVersion;
	}

	public void setSoftwareVersion(SoftwareVersion softwareVersion) {
		this.softwareVersion = softwareVersion;
	}
	
	public void onClick$menuitemFileTypeAdd(Event event) throws Exception {
		this.fileTypeAdd();
	}

	public void onClick$menuitemFileTypeRemove(Event event)
			throws Exception {
		this.fileTypeRemove();
	}
	
	public void onClick$toolbarbuttonFileTypeAdd(Event event) throws Exception {
		this.fileTypeAdd();
	}

	public void onClick$toolbarbuttonFileTypeRemove(Event event)
			throws Exception {
		this.fileTypeRemove();
	}

	@Override
	public void doAfterCompose(Component component) throws Exception {
		super.doAfterCompose(component);
		this.softwareVersion = (SoftwareVersion) this.execution
				.getAttribute("softwareVersion");
		
		this.fileTypes = new ArrayList<FileType>();
		
		this.saveData();
		
		component.setAttribute("controller", this);
	}

	private void fileTypeAdd() throws Exception {
		Window window = (Window) this.execution.createComponents(
				"~./zul/html/fileTypeSelect.zul", null, null);
		window.setParent(self);
		window.doModal();

		if (this.execution.getAttribute("result") == "ok") {
			FileType selectedFileType = (FileType) this.execution
					.getAttribute("selectedFileType");
			this.softwareVersion.getFileTypes().add(selectedFileType);
		}

		this.binder.loadComponent(this.listboxFileType);
	}

	private void fileTypeRemove() throws Exception {
		Set<Listitem> selectedItems = this.listboxFileType.getSelectedItems();

		for (Listitem selectedItem : selectedItems) {
			FileType selectedFileType = (FileType) selectedItem.getValue();
			this.softwareVersion.getFileTypes().remove(selectedFileType);
		}

		this.binder.loadComponent(this.listboxFileType);
	}

	public void onClick$buttonOk(Event event) throws Exception {
		if (this.softwareVersion.getName() != null) {
			this.execution.setAttribute("softwareVersion", this.softwareVersion);
			this.execution.setAttribute("result", "ok");
			this.self.detach();
		} else {
			Messagebox.show("You must enter 'Software Version Name'.");
		}
	}

	public void onClick$buttonCancel(Event event) throws Exception {
		this.restoreData();	
		
		this.execution.setAttribute("softwareVersion", null);
		this.execution.setAttribute("result", "cancel");
		this.self.detach();
	}

	private void saveData() {		
		
		for (FileType fileType : this.softwareVersion.getFileTypes()) {
			this.fileTypes.add(fileType);
		}
		
		this.name = this.softwareVersion.getName();
		this.description = this.softwareVersion.getDescription();
	}

	private void restoreData() {
		
		this.softwareVersion.getFileTypes().clear();
		
		for (FileType fileType : this.fileTypes) {
			this.softwareVersion.getFileTypes().add(fileType);
		}
		
		this.softwareVersion.setName(this.name);
		this.softwareVersion.setDescription(this.description);
	}
}
